package cn.picclife.mwx.manager.util;


import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.stereotype.Component;

@Component("redisCache")
public class RedisUtil{

	
	@Resource(name = "stringRedisTemplate")
	RedisTemplate<String, String> strRedisTemplate;

	public RedisTemplate<String, String> getStrRedisTemplate() {
		return strRedisTemplate;
	}
	
	public void saveZset(String key, List<String> setList, double time){
			this.getStrRedisTemplate().opsForZSet().add(key, setList.toString(), time);
	}
	
	public Set<String> getZset(String key){
		return this.getStrRedisTemplate().opsForZSet().range(key, 0, -1);
	}
	
	public Set<TypedTuple<String>> rangeWithScores(String key) {
		return this.getStrRedisTemplate().opsForZSet().rangeWithScores(key, 0, -1);
	}
	
	public void setStr(String key,String value) {
		this.getStrRedisTemplate().opsForValue().set(key, value);
	}
	
	public String getStr(String key) {
		return this.getStrRedisTemplate().opsForValue().get(key);
	}
	
	public void setExpire(String key,long time) {
		this.getStrRedisTemplate().expire(key, time, TimeUnit.MILLISECONDS);
	}
	
	public void delete(String key) {
		this.getStrRedisTemplate().delete(key);
	}
	
	public void rightPushList(String key,String value) {
		this.getStrRedisTemplate().opsForList().rightPush(key, value);
	}
	
	public List<String> getAllList(String key) {
		return this.getStrRedisTemplate().opsForList().range(key, 0, -1);
	}
	
	public void removeList(String key ,long i ,Object value) {
		this.getStrRedisTemplate().opsForList().remove(key, i, value);
	}
	
	public Long listSize(String key) {
		return this.getStrRedisTemplate().opsForList().size(key);
	}
	
	public Set<String> Keys(String pattern) {
		return this.getStrRedisTemplate().keys(pattern);
	}


}
